package com.gitee.center.runtime.def;

import com.gitee.center.runtime.IDomainExtension;
import com.gitee.center.runtime.IRegistryAware;
import com.gitee.center.runtime.InternalIndexer;
import com.gitee.center.runtime.annotation.Extension;
import com.gitee.center.runtime.tool.InternalAopUtils;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class ExtensionDef implements IRegistryAware {
    private String code;
    private String name;
    private Class<? extends IDomainExtension> extClazz;

    private IDomainExtension extensionBean;
    @Override
    public void registerBean(Object bean) {
        initialize(bean);
        InternalIndexer.index(this);
    }

    public ExtensionDef(IDomainExtension extensionBean) {
        this.extensionBean = extensionBean;
    }

    private void initialize(Object bean) {
        Extension extension = InternalAopUtils.getAnnotation(bean, Extension.class);
        this.code = extension.code();
        this.name = extension.name();

        this.extensionBean = (IDomainExtension) bean;
        for (Class extensionBeanInterfaceClazz : InternalAopUtils.getTarget(this.extensionBean).getClass().getInterfaces()) {
            if (extensionBeanInterfaceClazz.isInstance(extensionBean)) {
                this.extClazz = extensionBeanInterfaceClazz;
                break;
            }
        }
    }

}
